<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license         
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Blog_model
 *
 * Model used to retrive webites
 *
 * @package			conlabz CMS
 * @subpackage		Models
 */
class Person_model extends Base_model {
	function Person_model( ) {
		parent::Base_model( );

		$this->_TABLES = array( 'Option' => $this->config->item( 'backendpro_table_prefix' ) . 'article' );

		// Cache to store already fetched items
		$this->_CACHE = array( );

		// Object keyword
		// I wouldn't advise changing this, it could corrupt current
		// articles, the reason for needing this is to stop unserialze
		// errors spamming the log files
		$this->object_keyword = "BeP::Object::";

		log_message( 'debug', 'conlabz CMS : Person_model class loaded' );
	}

	/**
	 * Get Blogs
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getPersons( $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'person.id, 
							person.name, 
							person.firstname,
							personfunction.title' 
						 );
		$this->db->from( "person" );
		$this->db->from( "personfunction" );
		$this->db->from( "personfunction_xref" );
		$this->db->from( "category" );
		$this->db->from( "orgunit" );
		$this->db->from( "orgunit_xref" );
		$this->db->where( "`personfunction_xref`.`person_ref` = `person`.`id`
						   AND `personfunction_xref`.`personfunction_ref` = `personfunction`.`id`
						   AND `personfunction`.`id` = `orgunit_xref`.`function_ref`
						   AND `orgunit_xref`.`orgunit_ref` = `orgunit`.`id`
						   AND `orgunit`.`category_id` = `category`.`id` 
						   AND `orgunit`.`category_language_id` = `category`.`language_id` 
						   AND `category`.`website_id` = ", $this->session->userdata['website_id'] );
		if( !is_null( $where ) ) {
			$this->db->where( $where );
		}
		if( !is_null( $limit['limit'] ) ) {
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$this->db->order_by('person.name desc'); 
		return $this->db->get( );
	}
	
	/**
	 * Insert new Blog
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function insert( $data = array( ) ) {
		//create new person entry
		$this->db->set( 'id',          $data['id']          );
		$this->db->set( 'name',        $data['name']        );
		$this->db->set( 'firstname',      $data['firstname']      );
		$this->db->set( 'email',      $data['email']      );

		//insert the new blog in the blog DB
		$this->db->insert( 'person' );
		
		$person_id = $this->db->insert_id( );
		
		$this->db->set( 'personfunction_ref',          $data['personfunction_id']          );
		$this->db->set( 'person_ref',          		$person_id          );
		
		$this->db->insert( 'personfunction_xref' );
		
		return $person_id;
	}
	
	/**
	 * Update Blog
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function update( $data = array( ), $id ) {		
		$blog = array( );
		$blog['name']        = $data['name'];
		$blog['firstname']      = $data['firstname'];
		$blog['email']      = $data['email'];

		//insert the new blog
		$this->db->update( 'person', $person, array( 'id' => $id ) );
		//@todo: update personfunction_xref
	}

}
/* End of file person_model.php */
/* Location: ./modules/people/models/person_model.php */
